package com.suneast.suneaststore.modules.personal.collect;

import android.util.Log;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.suneast.suneaststore.API;
import com.suneast.suneaststore.app.AccountManager;
import com.suneast.suneaststore.modules.common.CommonUtil;
import com.suneast.suneaststore.bean.personal.collect.CollectGoodsBean;
import com.suneast.suneaststore.bean.personal.collect.CollectStoreBean;
import com.suneast.suneaststore.modules.personal.collect.event.GotCollectedGoodsListResult;
import com.suneast.suneaststore.modules.personal.collect.event.GotCollectedStoreListResult;
import com.suneast.suneaststore.modules.common.Flag;
import com.suneast.suneaststore.util.log.LatteLogger;
import com.suneast.suneaststore.util.net.RestClient;

import org.greenrobot.eventbus.EventBus;

import java.util.List;

public class CollectHandler {

    static void getCollectedGoodsList(int page) {
        RestClient.builder()
                .url(API.COLLECTED_GOODS_LIST)
                .params("token", AccountManager.getToken())
                .params("psize", 6)
                .params("page", page)
                .success(response -> {
                    LatteLogger.json("test", response);
                    JSONObject rootObj = JSON.parseObject(response);
                    if (rootObj.getIntValue("code") == 1) {
                        JSONObject dataObj = rootObj.getJSONObject("data");
                        List<CollectGoodsBean> beanList = JSON.parseArray(
                                dataObj.getJSONArray("list").toJSONString(),
                                CollectGoodsBean.class);
                        int pageCount = dataObj.getIntValue("page");
                        EventBus.getDefault().post(
                                new GotCollectedGoodsListResult(Flag.Result.SUCCEED, pageCount, beanList));
                    } else {
                        EventBus.getDefault().post(
                                new GotCollectedGoodsListResult(Flag.Result.FAILURE, -1, null));
                        LatteLogger.e("error", "getCollectedGoodsList response success, "
                                + "code = " + rootObj.getIntValue("code")
                                + " msg = " + rootObj.getString("msg"));
                    }
                })
                .error((code, msg) -> {
                    LatteLogger.e("error", "getCollectedGoodsList error, "
                            + "code = " + code
                            + " msg = " + msg);
                    EventBus.getDefault().post(
                            new GotCollectedGoodsListResult(Flag.Result.FAILURE, -1, null));
                })
                .failure(() -> {
                    LatteLogger.e("error", "getCollectedGoodsList failure");
                    EventBus.getDefault().post(
                            new GotCollectedGoodsListResult(Flag.Result.FAILURE, -1, null));
                })
                .build()
                .post();
    }

    static void getCollectedStoreList(int page) {
        RestClient.builder()
                .url(API.COLLECTED_STORE_LIST)
                .params("token", AccountManager.getToken())
                .params("psize", 6)
                .params("page", page)
                .success(response -> {
                    LatteLogger.json("test", response);
                    JSONObject dataObj = CommonUtil.parseDataFromRootJSON(response);
                    if (dataObj != null) {
                        List<CollectStoreBean> beanList = JSON.parseArray(
                                dataObj.getJSONArray("list").toJSONString(),
                                CollectStoreBean.class);
                        Log.e("test", "getCollectedStoreList: size = " + beanList.size());
                        int pageCount = dataObj.getIntValue("page");
                        EventBus.getDefault().post(
                                new GotCollectedStoreListResult(Flag.Result.SUCCEED, pageCount, beanList));
                    } else {
                        EventBus.getDefault().post(
                                new GotCollectedStoreListResult(Flag.Result.FAILURE, -1, null));
                        JSONObject rootObj = JSON.parseObject(response);
                        LatteLogger.e("error", "getCollectedStoreList response success, "
                                + "code = " + rootObj.getIntValue("code")
                                + " msg = " + rootObj.getString("msg"));
                    }
                })
                .error((code, msg) -> {
                    EventBus.getDefault().post(
                            new GotCollectedStoreListResult(Flag.Result.FAILURE, -1, null));
                    LatteLogger.e("error", "getCollectedStoreList error, "
                            + "code = " + code
                            + " msg = " + msg);
                })
                .failure(() -> {
                    EventBus.getDefault().post(
                            new GotCollectedStoreListResult(Flag.Result.FAILURE, -1, null));
                    LatteLogger.e("error", "getCollectedStoreList failure");
                })
                .build()
                .post();
    }
}
